/** ============================================================================================== **/
/**						GESTIONE DELLE VASCHE									 				   **/	
/** ============================================================================================== **/
//Servizio
BootApplication.service("MyTankService",
				    ['configuration', '$rootScope', 'ngDialog', 'DateService', 'log', 'DialogService', '$translate', '$http', '$q',
			 function(configuration ,  $rootScope ,  ngDialog ,  DateService,   log ,  DialogService ,  $translate ,  $http ,  $q) {
	  
	/**
	 * Questo metodo aggiorna la lista dei grafici da visualizzare in base ai filtri attivi
	 **/
	this.getTankList = function( _filtriAttivi, _displayProp ) {
		 _searchModel = {};
		 _searchModel.iDisplayStart		= _displayProp.displayStart!=null ? _displayProp.displayStart : 0;
		 _searchModel.iDisplayLength	= _displayProp.displayLength!=null ? _displayProp.displayLength : 5;
		 _searchModel.paginate			= _displayProp.paginate!=null ? _displayProp.paginate : false;
		 _searchModel.userId			= _filtriAttivi.userId;
		 if ( _filtriAttivi!=null) {
			 _searchModel.nome			= _filtriAttivi.nome;
			 _searchModel.larghezza 	= _filtriAttivi.larghezza;
			 _searchModel.lunghezza 	= _filtriAttivi.lunghezza;
			 _searchModel.altezza 		= _filtriAttivi.altezza;
			 _searchModel.capacita 		= _filtriAttivi.capacita;
		 }
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/searchTank.pjson"
				  ,data  	: _searchModel
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	}
	
	/**
	 * Questo metodo aggiorna la lista dei grafici da visualizzare in base ai filtri attivi
	 **/
	this.getTank = function( _tankId ) {
		_searchModel = {};
		_searchModel.id			= _tankId;
		var _defer = $q.defer();
		var request = {
				method	: 'POST'
				,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/searchTank.pjson"
				,data  	: _searchModel
		};
		$http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
		return _defer.promise;
	}
	
	/**
	 * Questo metodo salva la nuova vasca
	 **/
	this.saveTank = function( _tankModel ) {
		 _newModel = {};
		 if (_tankModel.id!=undefined) 				_newModel.id				= _tankModel.id;
		 if (_tankModel.idUser!=undefined) 			_newModel.idUser			= _tankModel.idUser;
		 if (_tankModel.nome!=undefined)			_newModel.nome 				= _tankModel.nome;
		 if (_tankModel.larghezza!=undefined) 		_newModel.larghezza 		= _tankModel.larghezza;
		 if (_tankModel.lunghezza!=undefined)		_newModel.lunghezza 		= _tankModel.lunghezza;
		 if (_tankModel.altezza!=undefined)			_newModel.altezza 			= _tankModel.altezza;
		 if (_tankModel.capacita!=undefined)		_newModel.capacita 			= _tankModel.capacita;
		 if (_tankModel.phMin!=undefined)			_newModel.phMin 			= _tankModel.phMin;
		 if (_tankModel.phMax!=undefined)			_newModel.phMax 			= _tankModel.phMax;
		 if (_tankModel.ghMin!=undefined)			_newModel.ghMin 			= _tankModel.ghMin;
		 if (_tankModel.ghMax!=undefined)			_newModel.ghMax 			= _tankModel.ghMax;
		 if (_tankModel.khMin!=undefined)			_newModel.khMin 			= _tankModel.khMin;
		 if (_tankModel.khMax!=undefined)			_newModel.khMax 			= _tankModel.khMax;
		 if (_tankModel.temperatureMin!=undefined)	_newModel.temperatureMin 	= _tankModel.temperatureMin;
		 if (_tankModel.temperatureMax!=undefined)	_newModel.temperatureMax 	= _tankModel.temperatureMax;
		 if (_tankModel.note!=undefined)			_newModel.note 				= _tankModel.note;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/saveTank.pjson"
				  ,data  	: _newModel
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	 }
	
	/**
	 * Questo metodo salva la nuova vasca
	 **/
	this.updateTank = function( _tankModel ) {
		 _newModel = {};
		 if (_tankModel.id!=undefined) 			_newModel.id			= _tankModel.id;
		 if (_tankModel.idUser!=undefined) 		_newModel.idUser		= _tankModel.idUser;
		 if (_tankModel.nome!=undefined)		_newModel.nome 			= _tankModel.nome;
		 if (_tankModel.larghezza!=undefined) 	_newModel.larghezza 	= _tankModel.larghezza;
		 if (_tankModel.lunghezza!=undefined)	_newModel.lunghezza 	= _tankModel.lunghezza;
		 if (_tankModel.altezza!=undefined)		_newModel.altezza 		= _tankModel.altezza;
		 if (_tankModel.capacita!=undefined)	_newModel.capacita 		= _tankModel.capacita;
		 if (_tankModel.note!=undefined)		_newModel.note 			= _tankModel.note;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/saveTank.pjson"
				  ,data  	: _newModel
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	 }
	
	/**
	 * Questo metodo elimina una vasca
	 **/
	this.deleteTank = function( _tankModel ) {
		 _delModel = {};
		 _delModel.idUser		= _tankModel.idUser;
		 _delModel.id 			= _tankModel.id;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/deleteTank.pjson"
				  ,data  	: _delModel
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	 }
	
	this.getTankTypes  = function( ) {
		var _defer = $q.defer();
		var request = {
				method	: 'POST'
				,url	: configuration.getParam("Constants").ContextPath + "tankCtrl/getTankTypes.pjson"
				,data  	: {}
		};
		$http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
		return _defer.promise;
	}
	
	
	/**
	 * Questo metodo recupera i task della vasca
	 **/
	this.getTask = function( _tankModel ) {
  	  	_newModel = {};
		 if (_tankModel.id!=undefined) 				_newModel.id				= _tankModel.id;
		 if (_tankModel.idUser!=undefined) 			_newModel.idUser			= _tankModel.idUser;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/getTask.pjson"
				  ,data  	: _newModel
		 };
 	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
 	  	 return _defer.promise;
	}
	
	/**
	 * Questo metodo recupera il livestock della vasca
	 **/
	this.getLivestock = function( _tankModel ) {
  	  	_newModel = {};
		 if (_tankModel.id!=undefined) 				_newModel.id				= _tankModel.id;
		 if (_tankModel.idUser!=undefined) 			_newModel.idUser			= _tankModel.idUser;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/getLivestock.pjson"
				  ,data  	: _newModel
		 };
 	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
 	  	 return _defer.promise;
	}
	/**
	 **/
	this.deleteLivestock = function( _livestock ) {
		 _delModel = {};
		 _delModel.id		= _livestock.id;
		 _delModel.type		= _livestock.type;
		 _delModel.tankId	= _livestock.tankId;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/deleteLivestock.pjson"
				  ,data  	: _delModel
		 };
  	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
  	  	 return _defer.promise;
	 }
	
	/**
	 * Questo metodo recupera il tank analisys della vasca
	 **/
	this.getTankAnalisys = function( _tankModel ) {
  	  	_newModel = {};
		 if (_tankModel.id!=undefined) 				_newModel.id				= _tankModel.id;
		 if (_tankModel.idUser!=undefined) 			_newModel.idUser			= _tankModel.idUser;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/getTankAnalisys.pjson"
				  ,data  	: _newModel
		 };
 	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
 	  	 return _defer.promise;
	}
	
	/**
	 * Questo metodo recupera i social network della vasca
	 **/
	this.getSocialNetwork = function( _tankModel ) {
  	  	_newModel = {};
		 if (_tankModel.id!=undefined) 				_newModel.id				= _tankModel.id;
		 if (_tankModel.idUser!=undefined) 			_newModel.idUser			= _tankModel.idUser;
		 
		 var _defer = $q.defer();
		 var request = {
				  method	: 'POST'
				  ,url		: configuration.getParam("Constants").ContextPath + "tankCtrl/getSocialNetwork.pjson"
				  ,data  	: _newModel
		 };
 	  	 $http(request).then(function(response){_defer.resolve(response);}, function(errorResponse){_defer.resolve(null);});
 	  	 return _defer.promise;
	}
}]);